package com.xxy.tree;

/**
 * @author xxy
 * @create 2021 1 24 14:11
 */
public class ArrBinaryTreeDemo {
    public static void main(String[] args) {
        int[] arr = new int[]{1,2,3,4,5,6,7};
        ArrBinaryTree arrBinaryTree = new ArrBinaryTree(arr);
        arrBinaryTree.preOrder();
    }
}
//编写一个ArrayBinaryTree，实现顺序存储二叉树遍历
class ArrBinaryTree{
    private int[] arr;//存储数据节点的数组
    public ArrBinaryTree(int [] arr){
        this.arr = arr;
    }
    public void preOrder(int index){
        if(arr==null||arr.length==0) {
            System.out.println("数组为空！");
            return;
        }
        //前序
        System.out.print(arr[index]+" ");
        if(2*index+1<arr.length){
            preOrder(2*index+1);
        }
        if(2*index+2<arr.length){
            preOrder(2*index+2);
        }
    }
    public void preOrder(){
        this.preOrder(0);
    }
}
